package com.mrjoew.weekend.configurations;

import com.mrjoew.weekend.services.sys.SysAccountService;
import com.mrjoew.weekend.services.sys.SysSecurityClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.store.redis.RedisTokenStore;

/**
 * @author Christopher Jones
 * @ClassName AuthorizationConfig
 * @explain Security 认证配置
 * @date 2020/6/17 0017 20:18
 */
@Configuration
@EnableAuthorizationServer
public class AuthorizationConfiguration extends AuthorizationServerConfigurerAdapter {

    private final SysAccountService sysAccountService;

    private final SysSecurityClientService sysSecurityClientService;

    private final AuthenticationManager authenticationManager;

    private final RedisConnectionFactory redisConnectionFactory;

    @Autowired
    public AuthorizationConfiguration(SysAccountService sysAccountService, SysSecurityClientService sysSecurityClientService, AuthenticationManager authenticationManager, RedisConnectionFactory redisConnectionFactory) {
        this.sysAccountService = sysAccountService;
        this.sysSecurityClientService = sysSecurityClientService;
        this.authenticationManager = authenticationManager;
        this.redisConnectionFactory = redisConnectionFactory;
    }

    @Value("${security.oauth2.resourceserver.jwt.converter.sign-key}")
    private String signKey;

    @Bean
    public JwtAccessTokenConverter accessTokenConverter() {
        JwtAccessTokenConverter converter = new JwtAccessTokenConverter() {
        };
        converter.setSigningKey(signKey);
        return converter;
    }

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients.withClientDetails(sysSecurityClientService::loadClientByClientId);
    }

    @Override
    public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
        security
                .tokenKeyAccess("permitAll()")
                .checkTokenAccess("isAuthenticated()")
                .allowFormAuthenticationForClients();
    }

    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) {
        endpoints
                .authenticationManager(authenticationManager)
                .userDetailsService(sysAccountService::loadUserByUsername)
                .tokenStore(new RedisTokenStore(redisConnectionFactory))
                .accessTokenConverter(accessTokenConverter())
                .reuseRefreshTokens(false);
    }
}